﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MX.Core.Caching.Memory
{
    /// <summary>
    /// 内存缓存的构造工厂
    /// </summary>
    public class MemoryCachingFactory : ICachingFactory
    {
        /// <summary>
        /// 获取缓存实例
        /// </summary>
        /// <returns></returns>
        public ICaching Default
        {
            get
            {
                return MemoryCaching.Default;
            }
        }

        /// <summary>
        /// 获取某个名称的缓存实例
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public ICaching CreateCaching(string name)
        {
            ICaching caching;
            if (!_cacheInstances.TryGetValue(name, out caching))
            {
                lock (_cacheInstances)
                {
                    if (!_cacheInstances.TryGetValue(name, out caching))
                    {
                        caching = new MemoryCaching(name);
                        _cacheInstances.Add(name, caching);
                    }
                }
            }
            return caching;
        }

        /// <summary>
        /// 缓存实例
        /// </summary>
        private Dictionary<string, ICaching> _cacheInstances = new Dictionary<string, ICaching>();

        /// <summary>
        /// 默认实例
        /// </summary>
        public readonly static ICachingFactory Instance = new MemoryCachingFactory();
    }
}
